﻿using PayPal.PayPalAPIInterfaceService;
using PayPal.PayPalAPIInterfaceService.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace DTcms.API.Payment.paypal
{
    public class PayPalReturn
    {
        public bool ResultVail(string token, string payerid, out string transactionID)
        {
            transactionID = "";
            PayPalAPIInterfaceServiceService services = PaypalConfig.GetPayService();
            GetExpressCheckoutDetailsResponseType getdtlResponse = services.GetExpressCheckoutDetails(new GetExpressCheckoutDetailsReq()
            {
                GetExpressCheckoutDetailsRequest = new GetExpressCheckoutDetailsRequestType()
                {
                    Token = token,
                    Version = "104.0"
                }
            });

            if (getdtlResponse.Ack == AckCodeType.SUCCESS || getdtlResponse.Ack == AckCodeType.SUCCESSWITHWARNING)
            {
                string payerid2 = getdtlResponse.GetExpressCheckoutDetailsResponseDetails.PayerInfo.PayerID;
                List<PaymentDetailsType> paymentDetails = getdtlResponse.GetExpressCheckoutDetailsResponseDetails.PaymentDetails.ToList();

                SellerDetailsType seller = new SellerDetailsType()
                {
                    PayPalAccountID = PaypalConfig.SellerEmail
                };

                //快速结帐付款请求详细信息
                var doExpressCheckoutPaymentRequestDetails = new DoExpressCheckoutPaymentRequestDetailsType()
                {
                    Token = token,
                    PayerID = payerid2,
                    PaymentAction = PaymentActionCodeType.SALE,
                    PaymentDetails = paymentDetails
                };
                //快速结帐付款请求
                var doExpressCheckoutPaymentRequest = new DoExpressCheckoutPaymentRequestType()
                {
                    Version = "104.0",
                    DoExpressCheckoutPaymentRequestDetails = doExpressCheckoutPaymentRequestDetails
                };

                var doExpressCheckoutPaymentReq = new DoExpressCheckoutPaymentReq()
                {
                    DoExpressCheckoutPaymentRequest = doExpressCheckoutPaymentRequest
                };

                paymentDetails.ForEach(model => model.SellerDetails = seller);

                DoExpressCheckoutPaymentResponseType payResponse = services.DoExpressCheckoutPayment(doExpressCheckoutPaymentReq);

                if (payResponse.Ack == AckCodeType.SUCCESS || payResponse.Ack == AckCodeType.SUCCESSWITHWARNING)
                {
                    var paymentInfo = payResponse.DoExpressCheckoutPaymentResponseDetails.PaymentInfo;

                    if (paymentInfo.Count > 0)
                    {
                        transactionID = paymentInfo[0].TransactionID;
                    }

                    return true;
                }
                else
                {
                    return false;
                }
            }
            return false;
        }
    }
}
